package com.flower.enjoyment.flower.service.impl;

import com.flower.enjoyment.flower.entity.ShopImg;
import com.flower.enjoyment.flower.entity.ShopInfo;
import com.flower.enjoyment.flower.entityDo.ServiceTypeDo;
import com.flower.enjoyment.flower.entityDo.ShopImgDo;
import com.flower.enjoyment.flower.entityDo.ShopInfoDo;
import com.flower.enjoyment.flower.entityDo.ShopServiceDo;
import com.flower.enjoyment.flower.mapper.ServiceTypeMapper;
import com.flower.enjoyment.flower.mapper.ShopImgMapper;
import com.flower.enjoyment.flower.mapper.ShopMapper;
import com.flower.enjoyment.flower.service.ShopService;
import com.flower.enjoyment.flower.utils.ImageUtil;
import com.flower.enjoyment.flower.utils.RandomUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
//@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ShopServiceImpl implements ShopService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ShopImgMapper shopImgMapper;
    @Autowired
    private ServiceTypeMapper serviceTypeMapper;

    /**
     * 获取推荐商铺列表
     *
     * @return
     */
    @Override
    public List<ShopInfoDo> getCommendShopHomeList() {
        return shopMapper.getCommendShopHomeList();
    }

    /**
     * 获取商铺列表
     * @return
     */
    public Page<ShopInfoDo> getCommendShopListByPage(Integer pageNo, Integer pageSize, String lon, String lat){
        PageHelper.startPage(pageNo, pageSize);
        return shopMapper.getCommendShopListByPage(lon,lat);
    }
    /**
     * 店铺详情
     * @param id
     * @return
     */
    public ShopInfoDo selectByPrimaryKey(String id){
        return shopMapper.selectByPrimaryKey(id);
    }

    /**
     * 申请店铺
     *
     * @param shopInfo
     * @return
     */
    @Override
    @Transactional
    public int saveShopInfo(ShopInfo shopInfo, List<ShopImgDo> imgs) throws Exception {
        shopInfo.setId(RandomUtil.getUuid());
        insertShopImg(imgs,shopInfo.getId());

        return shopMapper.insert(shopInfo);
    }

    /**
     * 保存图片
     * @param file
     * @param rootSavePath
     * @param rootHttpPath
     * @return
     * @throws Exception
     */
    @Override
    public String saveShopImg(MultipartFile file, String rootSavePath, String rootHttpPath,int type) throws Exception {
        if(type == 1) {
            //用户店铺图片
            rootSavePath += "/userShopInfoImg/";
            rootHttpPath += "/userShopInfoImg/";

        }else if (type == 2){
            rootSavePath += "/userAppointmentImg/";
            rootHttpPath += "/userAppointmentImg/";
        }
        logger.info("上传图片:name={} ", rootSavePath);
        logger.info("图片保存路径={}", rootSavePath);
        String fileName = ImageUtil.saveImg(file, rootSavePath);
        return rootHttpPath + fileName;
    }

    /**
     * 修改店铺
     *
     * @param shopInfo
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public int updateShopInfo(ShopInfo shopInfo, List<ShopImgDo> imgs) throws Exception {

        /*
        原设计（删除服务器上的图片
        List<ShopImg> shopImgs = shopImgMapper.selectImgListByShopId(shopInfo.getId());
        for(int i=0;i<shopImgs.size();i++){
            if (StringUtils.isBlank(shopImgs.get(i).getShortImg())) {
                ImageUtil.deleteImg(rootSavePath + shopImgs.get(i).getShortImg());
            }
        }*/

        shopImgMapper.selectImgListByShopId(shopInfo.getId());
        if(imgs!=null && imgs.size()>0) {
            insertShopImg(imgs, shopInfo.getId());
        }
        return shopMapper.updateByPrimaryKeySelective(shopInfo);
    }
    void insertShopImg(List<ShopImgDo> imgs,String shopId){
        for (int i=0;i<imgs.size();i++){
            ShopImg shopImg = new ShopImg();
            shopImg.setId(RandomUtil.getUuid());
            shopImg.setShopId(shopId);
            shopImg.setImg(imgs.get(i).getImg());
            shopImg.setSort(imgs.get(i).getSort());
            shopImgMapper.insert(shopImg);
        }
    }

    /**
     * 个人店铺信息
     *
     * @param userId
     * @return
     */
    @Override
    public ShopInfoDo selectByUserId(String userId) {
        return shopMapper.selectByUserId(userId);
    }

    /**
     * 常用店铺
     *
     * @param pageNo
     * @param pageSize
     * @param userId
     * @return
     */
    @Override
    public Page<ShopInfoDo> selectResortShopListByPage(Integer pageNo, Integer pageSize, String userId) {
        PageHelper.startPage(pageNo, pageSize);
        return shopMapper.selectResortShopListByPage(userId);
    }

    /**
     * 获取店铺服务列表
     *
     * @param pageNo
     * @param pageSize
     * @param shopId
     * @return
     */
    @Override
    public Page<ServiceTypeDo> getServiceTypeListByPage(Integer pageNo, Integer pageSize, String shopId) {
        PageHelper.startPage(pageNo, pageSize);
        return serviceTypeMapper.getServiceTypeListByPage(shopId);
    }

    /**
     * 获取门店服务项目
     *
     * @param pageNo
     * @param pageSize
     * @param shopId
     * @return
     */
    @Override
    public Page<ShopServiceDo> getShopServiceListByPage(Integer pageNo, Integer pageSize, String shopId) {
        PageHelper.startPage(pageNo, pageSize);
        return serviceTypeMapper.getShopServiceListByPage(shopId);
    }

    /**
     * 保存店铺服务
     *
     * @param shopService
     * @return
     */
    @Override
    @Transactional
    public int insertShopService(List<com.flower.enjoyment.flower.entity.ShopService> shopService) {
        serviceTypeMapper.deleteShopServiceByShopId(shopService.get(0).getShopId());
        for (com.flower.enjoyment.flower.entity.ShopService service : shopService) {
            service.setId(RandomUtil.getUuid());
            serviceTypeMapper.insertShopService(service);
        }
        return 1;
    }

    /**
     * 删除店铺服务
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int deleteShopService(String id) {
        return serviceTypeMapper.deleteShopService(id);
    }

    @Override
    public int selectCountByUserId(String userId) {
        return shopMapper.selectCountByUserId(userId);
    }


}
